//
//  NITimePickerView.m
//  gweAD
//
//  Created by navinfo air on 2017/2/21.
//  Copyright © 2017年 mengy. All rights reserved.
//

#import "NITimePickerView.h"

@implementation NITimePickerView
{
    
    __weak IBOutlet UIView *contentView;
    
    __weak IBOutlet UIPickerView *valuePickerView;
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
}



- (void)showWithTimeString:(NSString *)timeStr{
    [self setTimeForPickerView:timeStr];
    self.hidden = NO;
}

- (void)show{
    self.hidden = NO;
}

- (void)close{
    self.hidden = YES;
}

- (void)setTimeForPickerView:(NSString *)timeString{
    NSInteger h = [[[timeString componentsSeparatedByString:@":"] objectAtIndex:0] integerValue];
    NSInteger m = [[[timeString componentsSeparatedByString:@":"] objectAtIndex:1] integerValue];
    [valuePickerView selectRow:h inComponent:0 animated:NO];
    [valuePickerView selectRow:m/_minuteInterval inComponent:1 animated:NO];
}

#pragma mark - UIPickerView
- (IBAction)onCloseButtonOfPickViewClick:(id)sender{
    [self close];
}

- (IBAction)onDoneButtonClick:(id)sender{
    [self close];
    if (self.delegate) {
        NSString *h;
        if ([valuePickerView selectedRowInComponent:0] < 10) {
            h = [NSString stringWithFormat:@"0%ld",(long)[valuePickerView selectedRowInComponent:0]];
        }else{
            h = [NSString stringWithFormat:@"%ld",(long)[valuePickerView selectedRowInComponent:0]];
        }
        
        NSString *m;
        NSInteger tmpInteger = [valuePickerView selectedRowInComponent:1] * _minuteInterval;
        if (tmpInteger < 10) {
            m = [NSString stringWithFormat:@"0%ld",(long)tmpInteger];
        }else{
            m = [NSString stringWithFormat:@"%ld",(long)tmpInteger];
        }
        
        [valuePickerView selectedRowInComponent:1];
        NSString *selectTime = [NSString stringWithFormat:@"%@:%@",h,m];
        
        [self.delegate timeDidSelectedForPickerView:selectTime];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (![pickerView viewWithTag:1001]) {
        UILabel *unitLabel = [[UILabel alloc] init];
        unitLabel.text = @":";
        unitLabel.backgroundColor = [UIColor clearColor];
        unitLabel.textColor = [UIColor whiteColor];
        unitLabel.font = [UIFont systemFontOfSize:20];
        [unitLabel sizeToFit];
        [unitLabel setTag:1001];
        unitLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [pickerView addSubview:unitLabel];
        
        [pickerView addConstraint:[NSLayoutConstraint constraintWithItem:unitLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:pickerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
        [pickerView addConstraint:[NSLayoutConstraint constraintWithItem:unitLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:pickerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
        
    }
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 24;
    }
    if (_minuteInterval == 0) {
        _minuteInterval = 1;
    }
    return 60/_minuteInterval;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return [UIApplication sharedApplication].keyWindow.bounds.size.width/10;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    UIView *componentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width/2, [UIApplication sharedApplication].keyWindow.bounds.size.width/10)];
    UILabel *tempLabel = [[UILabel alloc] init];
    [componentView addSubview:tempLabel];
    tempLabel.textAlignment = NSTextAlignmentCenter;
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.textColor = [UIColor whiteColor];
    if (component == 0) {
        if (row < 10) {
            tempLabel.text = [NSString stringWithFormat:@"0%ld",(long)row];
        }else{
            tempLabel.text = [NSString stringWithFormat:@"%ld",(long)row];
        }
    }else{
        NSInteger tempInteger = row*_minuteInterval;
        if (tempInteger < 10) {
            tempLabel.text = [NSString stringWithFormat:@"0%ld",(long)tempInteger];
        }else{
            tempLabel.text = [NSString stringWithFormat:@"%ld",(long)tempInteger];
        }
    }
    
    
    if (row == [pickerView selectedRowInComponent:0]) {
        tempLabel.font = [UIFont systemFontOfSize:25];
    }else{
        tempLabel.font = [UIFont systemFontOfSize:2];
    }
    [tempLabel sizeToFit];
    NSInteger x;
    if (component == 0) {
        x = componentView.frame.size.width/2;
    }else{
        x = componentView.frame.size.width/2 - tempLabel.frame.size.width;
    }
    [tempLabel setFrame:CGRectMake(x, (componentView.frame.size.height - tempLabel.frame.size.height)/2, tempLabel.frame.size.width, tempLabel.frame.size.height)];
    //删除分割线
    for (UIView *subView in pickerView.subviews) {
        if (subView.frame.size.height <= 1 && subView.frame.size.width >= pickerView.frame.size.width) {
            [subView setFrame:CGRectMake(20, subView.frame.origin.y, subView.frame.size.width - 40, subView.frame.size.height)];
            [subView setBackgroundColor:[UIColor darkGrayColor]];
            [subView setAlpha:0.8f];
        }
        
    }
    return componentView;
}

- (void)setMinuteInterval:(NSInteger)minuteInterval{
    _minuteInterval = minuteInterval;
    [valuePickerView reloadAllComponents];
}

@end
